﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Me.Framework.ObjFactory
{
    /// <summary>
    /// 对象工厂
    /// </summary>
    public static class ObjectFactory
    {

        public static List<string> Ass { get; set; }

        public static T GetObject<T>() where T : class
        {
            T temp = default(T);
            Type type = typeof(T);
            foreach (var ass in Ass)
            {
                Assembly assembly = Assembly.Load(ass);
                Type[] types = assembly.GetTypes();
                foreach (var item in types)
                {
                    if (item.GetInterface(type.Name) != null)
                    {
                        temp = (T)Activator.CreateInstance(item);
                    }

                    if (temp != null)
                        break;
                }
            }

            if (temp == null)
            {
                throw new Exception("未找到接口的实现");
            }

            return temp;
        }

        public static bool CheckIsSysType(Type t)
        {
            if (t == typeof(Boolean) || t == typeof(Byte) || t == typeof(SByte) || t == typeof(Int16) || t == typeof(Int32) || t == typeof(Int64) ||
               t == typeof(UInt16) || t == typeof(UInt32) || t == typeof(int) || t == typeof(Char) || t == typeof(Double) || t == typeof(String) ||
               t == typeof(Enum) || t == typeof(float) || t == typeof(long))
                return true;

            return false;
        }

        public static bool CheckObjectType(Type t)
        {
            if (t is IConvertible)
                return true;
            return false;
        }

        public static bool CheckTypeIsCollection(Type t)
        {
            var temp = Activator.CreateInstance(t, null);
            if (temp is IEnumerable)
                return true;
            return false;
        }

        public static PropertyInfo[] GetTypeProperty(Type t)
        {
            PropertyInfo[] propertys = t.GetProperties();
            return propertys;
        }
    }
}
